﻿using System;
using System.IO;
using System.Linq;
class FileProcess
{
    //存取文件地址
    internal static Tuple<string, string, string> GetFile()
    {
        string input = @"C:\Users\leomojys\Desktop\input.txt";
        string output = @"C:\Users\leomojys\Desktop\output.txt";
        string word = @"C:\Users\leomojys\Desktop\words.txt";
        return Tuple.Create(input, output, word);
    }
    //处理文件
    internal static void ProcessFile(string input, string output, string word)
    {
        try
        {
            List<string> Lines = new List<string>();

            //读取敏感词
            Dictionary<string, string> sensitiveWords = SensitiveWordFilter.SensitiveWords(word);
            
            // 按行读取文件内容
            string[] passage = File.ReadAllLines(input);
          
            for (int i = 0; i < passage.Length; i++)
            {
                string ThisPart = passage[i];

                //先处理本行
                if (ThisPart.Length != 0)
                {
                    ThisPart = SensitiveWordFilter.FilterSensitiveWords(ThisPart, sensitiveWords);
                }

                string NextPart = "";
                if (i < passage.Length - 1)
                {
                    NextPart = passage[i + 1];
                }

                // 和下一行合并再做处理（如果有下一行）
                if (NextPart.Length > 0)
                {
                    string combinedPart = ThisPart + NextPart;
                    string filteredPart = SensitiveWordFilter.FilterSensitiveWords(combinedPart, sensitiveWords);
                    // 处理好后再复写到原本行
                    ThisPart = filteredPart.Substring(0, ThisPart.Length);
                    if (i < passage.Length - 1)
                    {
                        passage[i + 1] = filteredPart.Substring(ThisPart.Length);
                    }
                }

                Lines.Add(ThisPart);
            }

            string filteredPassage = String.Join("\n", Lines);

            // 写入新文件
            File.WriteAllText(output, filteredPassage);
            Console.WriteLine(filteredPassage);//命令窗口显示替换内容
        }
        catch (System.Exception ex)  //捕获异常
        {
            Console.WriteLine("Exception catch here - details  : " + ex.ToString());
        }
    }
}

class SensitiveWordFilter
{
    // 敏感词字典
    internal static Dictionary<string, string> SensitiveWords(string word)
    {
        Dictionary<string, string> sensitiveWords = new Dictionary<string, string>();
       
        string[] lines = File.ReadAllLines(word);//将每一行存储为字符串数组lines中的一个元素

        foreach (string line in lines)
        {
            string[] parts = line.Split(',');
            if (parts.Length == 2)
            {
                sensitiveWords.Add(parts[0].Trim(), parts[1].Trim());//将敏感词和替换词添加到字典中，同时使用Trim方法去除字符串两端的空白字符
            }
        }
        return sensitiveWords;
    }

    // 敏感词替换方法
    internal static string FilterSensitiveWords(string line, Dictionary<string, string> sensitiveWords)
    {
        foreach (var word in sensitiveWords)
        {
            string pattern = word.Key;
            string replacement = word.Value;
            line = System.Text.RegularExpressions.Regex.Replace(line, pattern, replacement);
        }
        return line;
    }   
}
public class Program
{
    static void Main(string[] args)
    {
        var filePaths = FileProcess.GetFile();
        // 从元组中提取文件路径
        string inputFilePath = filePaths.Item1;
        string outputFilePath = filePaths.Item2;
        string wordsFilePath = filePaths.Item3;

        FileProcess.ProcessFile(inputFilePath, outputFilePath, wordsFilePath);
    }
}